package com.example.lib_common

import android.app.Application
import com.alibaba.android.arouter.BuildConfig
import com.alibaba.android.arouter.launcher.ARouter
import com.example.lib_common.mmkv.AppMMKV
import com.example.lib_common.router.DegradeServiceImpl
import com.example.lib_common.utils.LoggerUtils
import com.example.lib_common.websocket.WebSocketUtils
import com.example.lib_common.websocket.WebSocketUtils.ReceiveMsgListener
import com.example.lib_storage.database.DBUtils
import com.tencent.bugly.crashreport.CrashReport
import com.tencent.mmkv.MMKV
import com.umeng.analytics.MobclickAgent
import com.umeng.commonsdk.UMConfigure
import org.java_websocket.client.WebSocketClient
import java.util.concurrent.Executors

/**
 * @author yaotianxue
 * @description
 * @date 2024/10/21
 */
open class BaseApplication: Application() {
    override fun onCreate() {
        super.onCreate()
        instance = this

    }
    companion object{
        lateinit var instance: BaseApplication
    }


    fun initWebSocket(listener: ReceiveMsgListener){
        WebSocketUtils.Builder()
            .setWebsocketUrl("")
            .setListener(listener)
            .build()
    }

    fun initMMKV() {
        MMKV.initialize(this)
    }

    fun initPreUmeng() {
        UMConfigure.setLogEnabled(true);
        // SDK预初始化函数,不会采集用户数据
        // preInit预初始化函数耗时极少，不会影响App首次冷启动用户体验
        UMConfigure.preInit(this, "67170cee80464b33f6e18739", "Umeng"); //必须调用
        // 自动采集选择，只能采集activity
//        MobclickAgent.setPageCollectionMode(MobclickAgent.PageMode.AUTO)
        //手动采集选择,采集activity、fragment、自定义view
        MobclickAgent.setPageCollectionMode(MobclickAgent.PageMode.MANUAL)
        //正式初始化，之前同意过，建议放到后台线程中延迟调用，避免影响App启动
        if (AppMMKV.splashAgreement){
            initUmeng()
        }
    }

     fun initUmeng(){
        Executors.newCachedThreadPool().submit {
            UMConfigure.init(this, "67170cee80464b33f6e18739", "Umeng", UMConfigure.DEVICE_TYPE_PHONE, "")
            LoggerUtils.d("App","初始化了友盟")
        }
    }

     fun initBugly() {
        //保证用户同意隐私协议后初始化，不然不合规
        //引导页用户点击同意后调用延迟初始化
        if (AppMMKV.splashAgreement){
            CrashReport.initCrashReport(this, "55f8220dd1", true);
            LoggerUtils.d("App","初始化了bugly")
        }
    }

     fun initArouter(){
        //ARouter初始化
         ARouter.openLog()
         ARouter.openDebug()
         ARouter.init(this)
         //注册降级策略
         ARouter.getInstance().navigation(DegradeServiceImpl::class.java)
         //日志使用
         LoggerUtils.d("app","初始化ARouter")
    }

    fun initRoom(){
        DBUtils.init(this)
    }


}